﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using Wicresoft.RDW.Interface;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Presentation
{
    public class BasePresent<K, F>
        where F : IObject<K>
        where K : EntityObject
    {
        protected static F present = IOC.Load<F>();

        public static K GetById(int id)
        {
            return present.GetById(id);
        }

        public static K GetByCondition(Expression<Func<K,bool>> condition) 
        {
            return present.GetByCondition(condition);
        }

        public static List<K> GetList()
        {
            return present.GetList();
        }

        public static List<K> GetList(Expression<Func<K, bool>> condition)
        {
            return present.GetList(condition);
        }

        public static PagedList<K> GetPaged(int pageIndex, int pageSize)
        {
            return present.GetPaged(pageIndex, pageSize);
        }

        public static PagedList<K> GetPaged(int pageIndex, int pageSize, Expression<Func<K, bool>> expression)
        {
            return present.GetPaged(pageIndex, pageSize, expression);
        }

        public static PagedList<K> GetPaged<TKey>(int pageIndex, int pageSize, Expression<Func<K, bool>> expression, Expression<Func<K, TKey>> orderBy, bool isDesc)
        {
            return present.GetPaged(pageIndex, pageSize, expression, orderBy, isDesc);
        }

        public static PagedList<K> GetPaged<TKey>(int pageIndex, int pageSize, Expression<Func<K, TKey>> orderBy, bool isDesc)
        {
            return present.GetPaged(pageIndex, pageSize, orderBy, isDesc);
        }

        public static PagedList<K> GetPaged(List<K> qry, int count, int pageIndex, int pageSize)
        {
            return present.GetPaged(qry, count, pageIndex, pageSize);
        }

        public static void Create(K k)
        {
            present.Create(k);
        }

        public static void Update(K k)
        {
            present.Update(k);
        }

        public static void Delete(int id)
        {
            var item = GetById(id);
            K k = Activator.CreateInstance<K>();
            item.CopeProperties(k, true);
            k.EntityKey = item.EntityKey;
            present.Delete(k);
        }

        public static void Delete(List<K> ts)
        {
            List<K> list = new List<K>();
            foreach (K item in ts)
            {
                 K k = Activator.CreateInstance<K>();
                 item.CopeProperties(k, true);
                 k.EntityKey = item.EntityKey;
                 list.Add(k);
            }
            present.Delete(list);
        }
    }
}
